import Pluto,struct,codecs,fnmatch,StringIO
fl=Pluto.walk('unpack')
for i in range(len(fl)):
        if fnmatch.fnmatch(fl[i],'*.txt'):
                continue
        fp=open(fl[i],'rb')
        offset=struct.unpack('i',fp.read(4))[0]*4+4
        tar=codecs.open(fl[i]+'.txt','wb','utf_16')
        start=0
        for j in range(4,offset,4):
                fp.seek(j)
                end=struct.unpack('i',fp.read(4))[0]
                size=end-start
                fp.seek(offset)
                dat=StringIO.StringIO(fp.read(size))
                text=[]
                for k in range(0,len(dat.read()),2):
                        dat.seek(k)
                        value=struct.unpack('H',dat.read(2))[0]
                        if value==0:
                                break
                        if value==0xd:
                                word='[0d00]'
                        elif value==0xa:
                                word='\r\n'
                        else:
                                word=unichr(value)
                        text.append(word)
                tar.write('//start\r\n'+''.join(text)+'\r\n//end\r\n\r\n')
                dat.close()
                offset+=size
                start=end
        fp.close()
        tar.close()
        print fl[i],'done!'
